package com.yihu.wlyy.repository;

import java.util.Date;
import java.util.List;

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import com.yihu.wlyy.entity.follow.FollowUpPlanStageItem;

public interface FollowUpPlanStageItemDao extends PagingAndSortingRepository<FollowUpPlanStageItem, Long>, JpaSpecificationExecutor<FollowUpPlanStageItem> {

	List<FollowUpPlanStageItem> findByPlanAndStage(String plan, String stage);

	FollowUpPlanStageItem findByCode(String code);

	/**
	 * 根据计划code查找出随访计划
	 * 
	 * @param code
	 * @return
	 */
	@Query("select a from FollowUpPlanStageItem a where a.plan = ?1 order by a.czrq desc")
	List<FollowUpPlanStageItem> findByPlanCode(String code);
	
	@Query("select a from FollowUpPlanStageItem a where a.stage = ?1")
	List<FollowUpPlanStageItem> findByStage(String stage);

	// 查询未结束的且到推送日期的随访项
	@Query("select a from FollowUpPlanStageItem a where a.nextPushDate = ?1 and a.status = 1")
	List<FollowUpPlanStageItem> findByNextPushDate(Date nextPushDate);
	
	@Modifying
	@Query("update FollowUpPlanStageItem a set a.status = ?1 where a.plan = ?2")
	int updateStatusByPlan(int status, String plan);
	
	@Modifying
	@Query("update FollowUpPlanStageItem a set a.status = ?1 where a.stage = ?2")
	int updateStatusByStage(int status, String stage);
	
	//查询阶段下是否还有未结束的随访项
	@Query("select count(1) from FollowUpPlanStageItem a where a.stage = ?1 and a.code <> ?2 and a.status <> 2")
	int hasOtherUnfinish(String stage, String item);

	@Query("select a from FollowUpPlanStageItem a where a.plan = ?1 and a.status = 1")
	List<FollowUpPlanStageItem> findByPlanCodeAndType(String planCode, int index);

	@Query("select a from FollowUpPlanStageItem a where a.code = ?1 ")
	List<FollowUpPlanStageItem> findByItem(String itemCode);


}
